/**   
 * Copyright © 2019 eSunny Info. Tech Ltd. All rights reserved.
 * 
 * 功能描述：
 * @Package: com.seccloud.tencentcloudapi.ext.cvm 
 * @author: Yannis   
 * @date: 2019年9月26日 下午3:26:51 
 */
package com.tce.cvm;

import java.util.List;
import java.util.TreeMap;

import org.apache.http.NameValuePair;

import com.alibaba.fastjson.JSONObject;
import com.http.BasicClient;
import com.http.HttpResponse;
import com.tce.TceUtils;

public class CVMClient extends BasicClient {
	
	private static final String CVM_URL = "cvm.api3.tce.piccit.com.cn";
	
	public static RunInstancesResponse createCVMResponse() throws Exception {
		RunInstancesRequest req = new RunInstancesRequest();
		req.setInstanceChargeType("POSTPAID_BY_HOUR");
		req.setInstanceType("S3.SMALL1");
		req.setImageId("img-8toqc6s3");
		req.setInstanceName("bj_kc_测试_pro_");
		req.setInstanceCount(2);
		
		JSONObject body = (JSONObject) JSONObject.toJSON(req);
		body.put("InstanceChargePrepaid.Period", 1);
		body.put("InstanceChargePrepaid.RenewFlag", "NOTIFY_AND_MANUAL_RENEW");
		
		body.put("SystemDisk.DiskType", "CLOUD_PREMIUM");
		body.put("SystemDisk.DiskSize", 50);
		
		// TODO DataDisk[]
		
		body.put("VirtualPrivateCloud.VpcId", "vpc-omskc9f0");
		body.put("VirtualPrivateCloud.SubnetId", "subnet-pdvk3065");
		
		body.put("Placement.Zone", "kc");
		
		body.put("LoginSettings.Password", "Asd@1234");

		
		System.out.println("body ====== " + body.toJSONString());
		
		String actionName = "RunInstances";
		
		try {
			TreeMap<String, Object> paramTree = TceUtils.fillTsfParam(body, actionName, "bj", CVM_URL, "POST", "2017-03-12");
			TreeMap<String, String> headers = TceUtils.fillHttpHead(CVM_URL);
			List<NameValuePair> bodyList = TceUtils.getParamListForBody(paramTree);
			HttpResponse resp = new CVMClient().post("https://" + CVM_URL, headers, bodyList);
			if(resp.getStatusCode() == 200) {
				if(resp.getBody().indexOf("Error") >= 0) {
					throw new Exception("create cvm Failed."+resp.getBody());
				}
				JSONObject bodyJson = JSONObject.parseObject(resp.getBody());
				RunInstancesResponse response = bodyJson.getJSONObject("Response").toJavaObject(RunInstancesResponse.class);
				return response;
			}else {
				throw new Exception("create cvm Failed."+resp.toString());
			}
		} catch (Exception e) {
			throw e;
		}
	}
	
	public static DescribeInstancesResponse getCvmInstances(String[] instanceIds) throws Exception {
		JSONObject bodyJson = null;
		if(null != instanceIds) {
			bodyJson = new JSONObject();
			for(int i = 0, j = instanceIds.length; i < j; i++) {
				bodyJson.put("InstanceIds." + i, instanceIds[i]);
			}
		}
		String actionName = "DescribeInstances";
		try {
			TreeMap<String, Object> paramTree = TceUtils.fillTsfParam(bodyJson, actionName, "bj", CVM_URL, "GET", "2017-03-12");
			TreeMap<String, String> headers = TceUtils.fillHttpHead(CVM_URL);
			
			String paramsStr = TceUtils.getParamForBody(paramTree);
			
			HttpResponse resp = new CVMClient().get("https://" + CVM_URL + "?" + paramsStr, headers);
			if(resp.getStatusCode() == 200) {
				if(resp.getBody().indexOf("Error") >= 0) {
					throw new Exception("describe instances Failed."+resp.getBody());
				}
				JSONObject respBodyJson = JSONObject.parseObject(resp.getBody());
				DescribeInstancesResponse response = respBodyJson.getJSONObject("Response").toJavaObject(DescribeInstancesResponse.class);
				return response;
			}else {
				throw new Exception("describe instances Failed."+resp.toString());
			}
		} catch (Exception e) {
			throw e;
		}
		
	}
	
	public static DescribeInstancesStatusResponse getCvmInstancesStatus(String[] instanceIds) throws Exception {
		JSONObject bodyJson = null;
		if(null != instanceIds) {
			bodyJson = new JSONObject();
			for(int i = 0, j = instanceIds.length; i < j; i++) {
				bodyJson.put("InstanceIds." + i, instanceIds[i]);
			}
		}
		String actionName = "DescribeInstancesStatus";
		try {
			TreeMap<String, Object> paramTree = TceUtils.fillTsfParam(bodyJson, actionName, "bj", CVM_URL, "GET", "2017-03-12");
			TreeMap<String, String> headers = TceUtils.fillHttpHead(CVM_URL);
			
			String paramsStr = TceUtils.getParamForBody(paramTree);
			
			HttpResponse resp = new CVMClient().get("https://" + CVM_URL + "?" + paramsStr, headers);
			if(resp.getStatusCode() == 200) {
				if(resp.getBody().indexOf("Error") >= 0) {
					throw new Exception("describe instances status Failed."+resp.getBody());
				}
				JSONObject respBodyJson = JSONObject.parseObject(resp.getBody());
				DescribeInstancesStatusResponse response = respBodyJson.getJSONObject("Response").toJavaObject(DescribeInstancesStatusResponse.class);
				return response;
			}else {
				throw new Exception("describe instances status Failed."+resp.toString());
			}
		} catch (Exception e) {
			throw e;
		}
		
	}
}
